﻿#ifndef ZFTPDEMODIALOG_H
#define ZFTPDEMODIALOG_H

#include <QDialog>
#include "ZFtp.h"
#include <QFile>
#include <QEventLoop>
#include <QMenu>

class QTreeWidgetItem;

namespace Ui {
class ZFtpDemoDialog;
}

class ZFtpDemoDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ZFtpDemoDialog(QWidget *parent = nullptr);
    ~ZFtpDemoDialog();

private:
    Ui::ZFtpDemoDialog *ui;
    ZFtpNS::ZFtp m_zftp;
    //QFile m_upload_file;
    //QFile m_download_file;
    QHash<QString, bool> isDirectory;
    QString m_severCurrentPath;
    QEventLoop  m_loop;

    //菜单控件
    QMenu*   popMenu;
    QAction* cdToParent;
    QAction* upload;
    QAction* download;
    QAction* reName;
    QAction* createFolder;
    QAction* removeFile;
    QAction* removeFolder;
    QAction* refresh;

    // 请求/响应对象
    //连接
    QSharedPointer<ZFtpNS::ConnectToHostRequest> m_connect_to_host_request;
    QSharedPointer<ZFtpNS::ConnectToHostResponse> m_connect_to_host_response;

    //关闭
    QSharedPointer<ZFtpNS::CloseFtpRequest> m_close_request;
    QSharedPointer<ZFtpNS::CloseFtpResponse> m_close_response;

    //登录
    QSharedPointer<ZFtpNS::LoginRequest> m_login_request;
    QSharedPointer<ZFtpNS::LoginResponse> m_login_response;

    //切换路径
    QSharedPointer<ZFtpNS::CdDirRequest> m_cddir_request;
    QSharedPointer<ZFtpNS::CdDirResponse> m_cddir_response;

    //删除文件
    QSharedPointer<ZFtpNS::DeleteFileRequest> m_deletefile_request;
    QSharedPointer<ZFtpNS::DeleteFileResponse> m_deletefile_response;

    //文件重命名
    QSharedPointer<ZFtpNS::RenameFileRequest> m_renamefile_request;
    QSharedPointer<ZFtpNS::RenameFileResponse> m_renamefile_response;

    //创建文件夹
    QSharedPointer<ZFtpNS::MkDirRequest> m_mkdir_request;
    QSharedPointer<ZFtpNS::MkDirResponse> m_mkdir_response;

    //下载文件
    QSharedPointer<ZFtpNS::DownloadFileRequest1> m_dlfile_request;
    QSharedPointer<ZFtpNS::DownloadFileResponse1> m_dlfile_response;

    //上传文件
    QSharedPointer<ZFtpNS::UploadFileRequest2> m_ulfile_request;
    QSharedPointer<ZFtpNS::UploadFileResponse2> m_ulfile_response;

    //获取目录
    QSharedPointer<ZFtpNS::ListDirRequest> m_ls_request;
    QSharedPointer<ZFtpNS::ListDirResponse> m_ls_response;

    //上传文件
    QSharedPointer<ZFtpNS::UploadFileRequest3> m_ulfile2_request;
    QSharedPointer<ZFtpNS::UploadFileResponse3> m_ulfile2_response;

    //控件槽函数
private slots:
    void on_pushButton_Login_clicked();
    void on_pushButton_Quit_clicked();
    void on_pushButton_Refush_clicked();
    void on_pushButton_Back_clicked();
    void on_uploadButton_clicked();
    void on_uploadButton_All_clicked();
    void on_cancelBtn_clicked();
    void on_toolButton_file_clicked();
    void on_toolButton_dir_clicked();
    void on_checkBox_clicked(bool checked);
    void on_radioButtonActive_clicked(bool checked);
    void on_radioButtonPassive_clicked(bool checked);
    void on_comboBox_coding_currentIndexChanged(const QString &arg1);
    void DoubleClicked_processItem(QTreeWidgetItem* item, int column);
    // 添加菜单
    void on_treeWidget_customContextMenuRequested(const QPoint);

    //自定义槽函数
private slots:
    //登录
    void slotConnectToHostResult(bool);
    void slotCloseFtpResult(bool);
    void slotLoginFtpResult(bool);
    //文件操作
    void slotCdDirFtpResult(bool);
//    void slotDeleteFileFtpResult(bool);
//    void slotRenameFileFtpResult(bool);
//    void slotMkDirFtpResult(bool);

//    void slotDownloadFileProgress(qint64 done, qint64 total);
//    void slotDownloadFileResult(bool);

    void slotUploadFileProgress(qint64 done, qint64 total);
    void slotUploadFileResult(bool);

    //list刷新
    void slotListInfo(QUrlInfo);
    void slotListInfoResult(bool);

    //状态改变
    void FtpStateChange(int state);



protected:
  void closeEvent(QCloseEvent* event);// 覆写closeEvent函数

private:
    void initWidget();  //初始化

    void ReadDefaultPath();      // 读写配置文件
    void WriteDefaultPath();

    void LogOutput_TextBrowser(const QString &msg,int colorState = 0);

    void sleep(unsigned int msec); // 延时功能

    void cdDir(QString dirPath);    //切换路径

    void lsDir();   //刷新

    void UploadDir(QString dirPath);
};

#endif // ZFTPDEMODIALOG_H

